<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author   
 *
 */
namespace osc\admin\controller;
use osc\common\controller\AdminBase;
use think\Db;
class Exception extends AdminBase{
	
	protected function _initialize(){
		parent::_initialize();
		$this->assign('breadcrumb1','异常流程审核');
		$this->assign('breadcrumb2','异常流程审核');		
	}

	function sendsms_normal($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_normal($mobile);
		return $response;
	}

	function sendsms_nopass($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_nopass($mobile);
		return $response;
	}

	function sendsms_exception($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_nopass($mobile);
		 
		return $response;
	}

	function index()
	{


		$param=input('param.');
		
		$query=[];
		
		if(isset($param['user_name'])){		
			$map['m.username|m.nickname']=['like',"%".$param['user_name']."%"];
			$query['m.username']=urlencode($param['user_name']);
		}
		/*else{
			$map['m.uid']=['gt',0];
		}
		*/
		
		$list=[];
		
		$list=Db::name('rang_order')->alias('m')->field('m.*')->where('rang>1')->where('exception_state','1')->where('exception_admin_state','0')
		->order('m.creat_time desc')->paginate(config('page_num'),false,$query);		
		//查询交易所
		$exchange = Db::name('exchange_house')->where('state',1)->select();
		$this->assign('exchange',$exchange);
		
		$this->assign('list',$list);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		$this->assign('breadcrumb1','异常流程');
		$this->assign('breadcrumb2','异常流程初审');
    	return $this->fetch();



				//获取未提现的记录
		//$wd=Db::name('auth_group_access')->where('pay_status','1')->order('creat_time desc')->select();


		//return $this->fetch();

	}

	function admin()
	{


		$param=input('param.');
		
		$query=[];
		
		if(isset($param['user_name'])){		
			$map['m.username|m.nickname']=['like',"%".$param['user_name']."%"];
			$query['m.username']=urlencode($param['user_name']);
		}
		/*else{
			$map['m.uid']=['gt',0];
		}
		*/
		
		$list=[];
		
		$list=Db::name('rang_order')->alias('m')->field('m.*')->where('rang>1')->where('exception_state','1')->where('exception_admin_state','1')
		->order('m.creat_time desc')->paginate(config('page_num'),false,$query);		
		//查询交易所
		$exchange = Db::name('exchange_house')->where('state',1)->select();
		$this->assign('exchange',$exchange);
		
		$this->assign('list',$list);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		$this->assign('breadcrumb1','异常流程');
		$this->assign('breadcrumb2','异常流程终审');
		
    	return $this->fetch();



				//获取未提现的记录
		//$wd=Db::name('auth_group_access')->where('pay_status','1')->order('creat_time desc')->select();


		//return $this->fetch();

	}


	function exception_his()
	{


		$param=input('param.');
		
		$query=[];
		
		if(isset($param['user_name'])){		
			$map['m.username']=['like',"%".$param['user_name']."%"];
			$query['m.username']=urlencode($param['user_name']);
		}
		/*else{
			$map['m.uid']=['gt',0];
		}
		*/
		
		$list=[];
		
		$list=Db::name('rang_order')->alias('m')->field('m.*')->where('rang>1')->where('exception_state','1')->where('exception_admin_state','1')
		->order('m.creat_time desc')->paginate(config('page_num'),false,$query);		
		//查询交易所
		$exchange = Db::name('exchange_house')->where('state',1)->select();
		$this->assign('exchange',$exchange);
		
		$this->assign('list',$list);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		$this->assign('breadcrumb1','异常流程');
		$this->assign('breadcrumb2','异常流程终审');
		
    	return $this->fetch();



				//获取未提现的记录
		//$wd=Db::name('auth_group_access')->where('pay_status','1')->order('creat_time desc')->select();


		//return $this->fetch();

	}

	function apply()
	{


		$id='';
		if(request()->isGet())
		{
			$data = input('get.');
			$id = $data['id'];
		}
		//获取数据配置
		//$buy_day = Db::name('config')->where('name','buy_day')->find();
		//$sell_day = Db::name('config')->where('name','sell_day')->find();
		
		if(request()->isPost()){
		
			$date=input('post.');
			$id=$date['id'];

			if($date['checked']=='1')
			{
				$rang['id']=$date['id'];
				//$rang['exchange_id']=$date['exchange_id'];
				//$rang['exchange_variety_id']=$date['variety_id'];
				//$rang['buy_price']=$date['buy_price'];
				//$rang['sell_time']=$date['sell_time'];
				//$rang['sell_price']=$date['sell_price'];	
				//$rang['sell_num']=$date['sell_num'];
				//$rang['buy_time'] = time()+($buy_day['value']*86400);
				//$rang['sell_time'] = time()+($sell_day['value']*86400);
				$rang['exception_admin_state'] = 1;
				$rang['exception_apply_desc'] = $date['exception_apply_desc'];
				$ret=Db::name('rang_order')->where('id',$id)->update($rang);


				$desc['rang_id']=$date['id'];
				$desc['desc']=$date['exception_apply_desc'];
				$desc['operator']="异常处理初级管理员";	
				$desc['state']="异常审核通过";					
				$ret=Db::name('rang_desc')->insert($desc);			
				$this->redirect('exception/index');

			}

			if($date['checked']=='2')
			{

				$desc['rang_id']=$date['id'];
				$desc['desc']=$date['exception_apply_desc'];
				$desc['operator']="管理员";	
				$desc['state']="异常审核不通过";					
				$ret=Db::name('rang_desc')->insert($desc);	

				$dt['exception_state'] = 3;
				//$dt['state'] = 0;
				$dt['exception_apply_desc'] = $date['exception_apply_desc'];
				$ret=Db::name('rang_order')->where('id',$id)->update($dt);
				$this->redirect('exception/index');
			}
			
		
		}



		$rang_order = Db::name('rang_order')->where('id',$id)->find();
		$img0 = Db::name('rang_order_img')->where('rang_order_id',$id)->where('seq_id',4)->select();
		//$rang_order['buy_time'] = time()+($buy_day['value']*86400);
		//$rang_order['sell_time'] = time()+($sell_day['value']*86400);
		$list=[
			'info'=>$rang_order
		];

		//var_dump($img0);


		$exchange =Db::name('exchange_house')->where('state',1)->select();
		$variety = Db::name('exchage_variety')->where('state',1)->select();


		
		$this->assign('data',$list);
		$this->assign('exchange',$exchange);
		$this->assign('variety',$variety);
		$this->assign('img0',$img0);
		$this->assign('group',Db::name('member_auth_group')->field('id,title')->select());
		$this->assign('crumbs','异常处理初审');
	 	return $this->fetch();

	}

	function admin_apply()
	{


		$id='';
		if(request()->isGet())
		{
			$data = input('get.');
			$id = $data['id'];
		}
		//获取数据配置
		//$buy_day = Db::name('config')->where('name','buy_day')->find();
		//$sell_day = Db::name('config')->where('name','sell_day')->find();
		
		if(request()->isPost()){
		
			$date=input('post.');
			$id=$date['id'];

			if($date['checked']=='1')
			{
				$rang['id']=$date['id'];
				//$rang['exchange_id']=$date['exchange_id'];
				//$rang['exchange_variety_id']=$date['variety_id'];
				//$rang['buy_price']=$date['buy_price'];
				//$rang['sell_time']=$date['sell_time'];
				//$rang['sell_price']=$date['sell_price'];	
				//$rang['sell_num']=$date['sell_num'];
				//$rang['buy_time'] = time()+($buy_day['value']*86400);
				//$rang['sell_time'] = time()+($sell_day['value']*86400);
				$rang['exception_admin_state'] = 1;
				$rang['exception_state'] = 2;
				$rang['state'] = 2;
				$rang['sell_state'] = 2;
				$rang['buy_state'] = 2;
				$rang['exception_apply_desc'] = $date['exception_apply_desc'];
				$ret=Db::name('rang_order')->where('id',$id)->update($rang);


				$desc['rang_id']=$date['id'];
				$desc['desc']=$date['exception_apply_desc'];
				$desc['operator']="异常处理管理员";	
				$desc['state']="异常审核通过";					
				$ret=Db::name('rang_desc')->insert($desc);			
				$this->redirect('exception/admin');

			}

			if($date['checked']=='2')
			{

				$desc['rang_id']=$date['id'];
				$desc['desc']=$date['exception_apply_desc'];
				$desc['operator']="管理员";	
				$desc['state']="异常审核不通过";					
				$ret=Db::name('rang_desc')->insert($desc);	

				$dt['exception_state'] = 3;
				$dt['exception_admin_state'] = 0;
				//$dt['state'] = 0;
				$dt['exception_apply_desc'] = $date['exception_apply_desc'];
				$ret=Db::name('rang_order')->where('id',$id)->update($dt);
				$this->redirect('exception/admin');
			}
			
		
		}



		$rang_order = Db::name('rang_order')->where('id',$id)->find();
		$img0 = Db::name('rang_order_img')->where('rang_order_id',$id)->where('seq_id',4)->select();
		//$rang_order['buy_time'] = time()+($buy_day['value']*86400);
		//$rang_order['sell_time'] = time()+($sell_day['value']*86400);
		$list=[
			'info'=>$rang_order
		];

		//var_dump($img0);


		$exchange =Db::name('exchange_house')->where('state',1)->select();
		$variety = Db::name('exchage_variety')->where('state',1)->select();


		
		$this->assign('data',$list);
		$this->assign('exchange',$exchange);
		$this->assign('variety',$variety);
		$this->assign('img0',$img0);
		$this->assign('group',Db::name('member_auth_group')->field('id,title')->select());
		$this->assign('crumbs','异常处理终审');
	 	return $this->fetch();

	}

	function init_state()
	{
		$id='';
		if(request()->isGet())
		{
			$data = input('get.');
			$id = $data['id'];
		}
		//获取数据配置
		$buy_day = Db::name('config')->where('name','buy_day')->find();
		$sell_day = Db::name('config')->where('name','sell_day')->find();
		

		$begin_time = "";
		//如果当前时间在13:00前,交易时间在当前15:00前完成
		$time = date("G");
		//var_dump($time);
		if($time<=12)
		{
			$begin_time = date("Y-m-d")." 15:00:00";
		}else
		{
			//获取当前是星期几，如果是星期五，只能是下周
			$da = date("w"); 
			//var_dump($da);
			$begin_time= date("Y-m-d",strtotime("+1 day"))." 13:00:00";
			if($da==5)
			{
				//当前时间+2天
				$begin_time= date("Y-m-d",strtotime("+3 day"))." 13:00:00";
			}
			if($da==6)
			{
				//当前时间+1天
				$begin_time =date("Y-m-d",strtotime("+2 day"))." 13:00:00";
			}
		}

		$end_time = date("Y-m-d",strtotime("+6 day"))." 15:00:00";
		if(request()->isPost()){
		
			$date=input('post.');
			$id=$date['id'];


			
				//$rang['id']=$date['id'];
				//$rang['exchange_id']=$date['exchange_id'];
				//$rang['exchange_variety_id']=$date['variety_id'];
				//$rang['admin_state'] = 1;
				$rang['id']=$date['id'];
				//$rang['exchange_id']=$date['exchange_id'];
				//$rang['exchange_variety_id']=$date['variety_id'];
				$rang['buy_num']=$date['buy_num'];
				$rang['buy_price']=$date['buy_price'];
				$rang['buy_time']=$begin_time;
				$rang['sell_time']=$end_time;
				$rang['sell_price']=$date['sell_price'];	
				$rang['sell_num']=$date['sell_num'];
				//$rang['buy_time'] = time()+($buy_day['value']*86400);
				//$rang['sell_time'] = time()+($sell_day['value']*86400);
				$rang['exception_state'] = 0;
				$rang['exception_admin_state'] = 0;
				$rang['buy_state'] = 0;
				$rang['sell_state'] = 0;
				$rang['state'] = 0;
				$rang['admin_state'] = 0;
				$rang['desc']=$date['desc'];
				$ret=Db::name('rang_order')->where('id',$id)->update($rang);

				$dt = Db::name('rang_order')->where('id',$id)->find();

				/*
				$rang_dt['username'] = $dt['username'];
				$rang_dt['trade_id'] = $dt['trade_id'];
				$rang_dt['total_time'] = $dt['total_time'];
				$rang_dt['exchange_id'] = $dt['exchange_id'];
				$rang_dt['rang'] = $dt['rang']+1;
				$rang_dt['creat_time'] = time();
				$rang_dt['exchange_variety_id'] = $dt['exchange_variety_id'];
				$rang_dt['id_name'] = $dt['id_name'];
				$rang_dt['id_card'] = $dt['id_card'];
				$ret=Db::name('rang_order')->insert($rang_dt);
				*/
				$desc['rang_id']=$date['id'];
				$desc['desc']=$date['desc'];
				$desc['operator']="管理员设置本轮为初始状态";	
				$desc['state']="审核通过";					
				$ret=Db::name('rang_desc')->insert($desc);	


				$this->redirect('exception/admin');

			
		
		}



		$rang_order = Db::name('rang_order')->where('id',$id)->find();
		$img0 = Db::name('rang_order_img')->where('rang_order_id',$id)->where('seq_id',0)->select();
		$img1 = Db::name('rang_order_img')->where('rang_order_id',$id)->where('seq_id',1)->select();
		$img2 = Db::name('rang_order_img')->where('rang_order_id',$id)->where('seq_id',2)->select();

		


		$rang_order['buy_time'] = $begin_time;
		$rang_order['sell_time'] = $end_time;
		$list=[
			'info'=>$rang_order
		];



		$exchange =Db::name('exchange_house')->where('state',1)->select();
		$variety = Db::name('exchage_variety')->where('state',1)->select();


		
		$this->assign('data',$list);
		$this->assign('exchange',$exchange);
		$this->assign('variety',$variety);
		$this->assign('group',Db::name('member_auth_group')->field('id,title')->select());
		$this->assign('crumbs','异常流程处理,重置用户排单');
	 	return $this->fetch();
	}
	


	


}
?>